package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.common.Common;
import com.lottery.ruok.lottrey.retrofitModule.response.GetServiceResponse;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import glideHelp.GlideHelper;
import utils.Utils;

/**
 * Created by ASUS on 2017/11/6.
 */

public class WeChatAdapter extends BaseAdapter {
    private List<GetServiceResponse.WechatBean> weChatList;
    private Context mContext;

    public interface SaveLocal{
        void saveLocal(int position);
    }

    private SaveLocal saveLocal;

    public void setSaveLocal(SaveLocal saveLocal){
        this.saveLocal = saveLocal;
    }

    public WeChatAdapter(List<GetServiceResponse.WechatBean> qqList, Context context){
        this.weChatList = qqList;
        mContext = context;
    }

    @Override
    public int getCount() {
        if(weChatList == null){
            return 0;
        }
        return weChatList.size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.wechat_item, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }

        holder.number.setText(weChatList.get(i).getAccount());
        GlideHelper.loadImage(mContext, weChatList.get(i).getImage(), holder.qrcode);
        holder.copy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.showToast(Common.COPY_TIP);

                Utils.copy(weChatList.get(i).getAccount(), mContext);
            }
        });
        holder.qrcode.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(saveLocal != null){
                    saveLocal.saveLocal(i);
                }
                return false;
            }
        });

        return view;
    }

    static class ViewHolder {
        @BindView(R.id.number)
        TextView number;
        @BindView(R.id.copy)
        TextView copy;
        @BindView(R.id.qrcode)
        ImageView qrcode;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
